<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<link rel="stylesheet" type="text/css" href="view/bootstrap-3.3.5/css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="view/bootstrapvalidator/css/bootstrapValidator.css"/>
<script charset="utf-8" src="view/jquery/jquery-1.10.2.min.js"></script>
<script charset="utf-8" src="view/bootstrap-3.3.5/js/bootstrap.js"></script>
<script charset="utf-8" src="view/bootstrapvalidator/js/bootstrapValidator.js"></script>


<div class="modal fade" id="addadjustmentModal" tabindex="-1"
	role="dialog" aria-labelledby="addadjustmentModalLabel"
	aria-hidden="true">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="addadjustmentModalLabel">员工调动</h4>
			</div>
			<form action="" class="form-horizontal" role="form" id="addAdjustmentForm">
				<div class="modal-body">
					<div class="row">
						<div class="form-group col-md-6">
							<label class="col-md-4 control-label">员工编号</label>
							<div class="col-md-8">
								<input type="text" id="empidText" class="form-control" disabled>
								<input type="hidden" name="empid" id="empid">
							</div>
						</div>

						<div class="form-group col-md-6">
							<label class="col-md-4 control-label">员工姓名</label>
							<div class="col-md-8">
								<input type="text" id="empNameText" class="form-control"
									disabled>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="form-group col-md-6">
							<label class="col-md-4 control-label">调动前部门</label>
							<div class="col-md-8">
								<input type="text" id="originDeptText" class="form-control"
									disabled> <input type="hidden" name="origindeptid"
									id="origindeptid">
							</div>
						</div>

						<div class="form-group col-md-6">
							<label class="col-md-4 control-label">调动前职位</label>
							<div class="col-md-8">
								<input type="text" id="originJobText" class="form-control"
									disabled> <input type="hidden" name="originjobid"
									id="originjobid">
							</div>
						</div>
					</div>
					<div class="row">
						<div class="form-group col-md-6">
							<label for="destdeptid" class="col-md-4 control-label">调动后部门</label>
							<div class="col-md-8">
								<select name="destdeptid" class="form-control" id="destdeptid">
									<option value="">--请选择--</option>
								</select>
							</div>
						</div>

						<div class="form-group col-md-6">
							<label for="destjobid" class="col-md-4 control-label">调动后职位</label>
							<div class="col-md-8">
								<select name="destjobid" class="form-control" id="destjobid">
									<option value="">--请选择--</option>
								</select>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="form-group col-md-12">
							<label for="reason" class="col-md-2 control-label">调动原因&nbsp;&nbsp;</label>
							<div class="col-md-9">
								<textarea class="form-control" id="reason" name="reason"
									rows="5" placeholder="请输入调动原因"></textarea>
							</div>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" id="sub" class="btn btn-primary">确认调动</button>
				</div>
			</form>
		</div>
	</div>
</div>

<script>
function addAdjustment(oid){
	
	$.getJSON("adjust/findEmpSimpleInfo",{"oid":oid}, function(emp) {
		$("#empidText").val(emp.oid);
		$("#empid").val(emp.oid);
		$("#empNameText").val(emp.empname);
		$("#originDeptText").val(emp.startdeptname);
		$("#origindeptid").val(emp.startdeptid);
		$("#originJobText").val(emp.startjobname);
		$("#originjobid").val(emp.startjobid);
		$('#addadjustmentModal').modal('show');
		
	}).complete(function(xhr) {
		if (xhr.status == "403") {//没有权限
			document.location.href = xhr.getResponseHeader("url");
		} else if (xhr.status != "200") {
			console.log("服务器错误!");
		}
	});
	
}
$(document).ready(function() {

	$.getJSON("dept/listDeptUseSelect", function(listdept) {
		var dept = $("#destdeptid")[0];
		dept.options.length = 1;
		for (var i = 0; i < listdept.length; i++) {
			var d = listdept[i];
			dept.options.add(new Option(d.deptName, d.oid));
		}
	}).complete(function(xhr) {
		if (xhr.status == "403") {//没有权限
			document.location.href = xhr.getResponseHeader("url");
		} else if (xhr.status != "200") {
			console.log("服务器错误!");
		}
	});

	$("#destdeptid").change(function() {
		if (this.value == "") {
			$("#destjobid")[0].options.length = 1;
			return;
		}
		$.getJSON("dept/listDeptJobUseSelect", {
			deptid : this.value
		}, function(listjob) {
			var job = $("#destjobid")[0];
			job.options.length = 1;
			for (var i = 0; i < listjob.length; i++) {
				var job1 = listjob[i];
				job.options.add(new Option(job1.name, job1.oid));
			}
		}).complete(function(xhr) {
			if (xhr.status == "403") {//没有权限
				document.location.href = xhr.getResponseHeader("url");
			} else if (xhr.status != "200") {
				console.log("服务器错误!");
			}
		});

	});

	$("#sub").click(function() {
		$('#addAdjustmentForm').data("bootstrapValidator").validate();
		if($('#addAdjustmentForm').data("bootstrapValidator").isValid()){
			if($("#origindeptid").val()==$("#destdeptid").val() && $("#originjobid").val()==$("#destjobid").val()){
				alert("调动岗位没有改变！");
			}else{
				$.post("adjust/addAdjustment", {
					'empid' : $("#empid").val(),
					'origindeptid' : $("#origindeptid").val(),
					'destdeptid' : $("#destdeptid").val(),
					'originjobid' : $("#originjobid").val(),
					'destjobid' : $("#destjobid").val(),
					'reason' :$("#reason").val()
				}, function(s) {
					if(s==true){
						document.location.href="adjust/adjustmentInfo?empid="+$("#empid").val();
					}else{
						$("#alertmsg").text("调动失败!");
				        $("#alertmsg").slideDown();
				        $("#alertmsg").delay(2000).slideUp();
					}
				}).complete(function(xhr) {
					if (xhr.status == "403") {//没有权限
						document.location.href = xhr.getResponseHeader("url");
					} else if (xhr.status != "200") {
						console.log("服务器错误!");
					}
				});
			}
		}
	});

	$('#addadjustmentModal').on('show.bs.modal', function() {
		$("#destdeptid").val("");
		$("#destjobid")[0].options.length = 1;
		$("#reason").val("");
	});
	
	$("#addAdjustmentForm").bootstrapValidator({
		message : 'This value is not valid',
		feedbackIcons : {/*input状态样式图片*/
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating : 'glyphicon glyphicon-refresh'
		},
		fields : {
			destdeptid : {
				validators : {
					notEmpty : {/*非空提示*/
						message : '请选择调动后部门'

					}
				}
			},
			destjobid : {
				validators : {
					notEmpty : {/*非空提示*/
						message : '请选择调动后职位'

					}
				}
			},
			reason : {
				validators : {
					stringLength : {
						max : 100,
						message : '调动原因长度在100位以内'
					}
				}
			}
		},
		submitHandler : function(validator,form, submitButton) {
			validator.defaultSubmit();
		}
	});
});
</script>